package com.hansming.bss.modules.sfc.controller;


import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.activiti.engine.impl.util.json.JSONArray;
import org.activiti.engine.impl.util.json.JSONException;
import org.activiti.engine.impl.util.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.impl.util.json.JSONArray;
import org.activiti.engine.impl.util.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hansming.bss.common.config.Global;
import com.hansming.bss.common.utils.IdGen;
import com.hansming.bss.common.utils.PropertiesLoader;
import com.hansming.bss.common.web.BaseController;

import com.hansming.bss.modules.sfc.entity.Bannar;
import com.hansming.bss.modules.sfc.entity.City;
import com.hansming.bss.modules.sfc.service.BannarService;
import com.hansming.bss.modules.sfc.service.CityService;
import com.hansming.bss.modules.sfc.service.ContactsService;
import com.hansming.bss.modules.sfc.service.CustomerScoreLogService;
import com.hansming.bss.modules.sfc.service.CustomerService;
import com.hansming.bss.modules.sfc.service.LineBranchService;
import com.hansming.bss.modules.sfc.service.LineDatePriceService;
import com.hansming.bss.modules.sfc.service.LineDaysDetailService;
import com.hansming.bss.modules.sfc.service.LineDaysService;
import com.hansming.bss.modules.sfc.service.LineExtrService;
import com.hansming.bss.modules.sfc.service.LineService;
import com.hansming.bss.modules.sfc.service.OrderItemService;
import com.hansming.bss.modules.sfc.service.OrderService;
import com.hansming.bss.modules.sfc.service.OrderTravelleridService;
import com.hansming.bss.modules.sfc.service.PcUserService;
import com.hansming.bss.modules.sfc.service.RichtextService;
import com.hansming.bss.modules.sfc.service.TravellerService;
import com.hansming.bss.modules.sfc.util.CarTimeHelp;
import com.hansming.bss.modules.sfc.util.LineDatePriceCountHelper;
import com.hansming.bss.modules.sfc.util.PageBean;
import com.hansming.bss.modules.sfc.util.PhoneMsgUtil;
import com.hansming.bss.modules.sfc.util.SendMailUtil;
import com.hansming.bss.modules.sfc.util.TimeUtil;
import com.hansming.bss.modules.sfc.entity.Contacts;
import com.hansming.bss.modules.sfc.entity.Customer;
import com.hansming.bss.modules.sfc.entity.CustomerScoreLog;
import com.hansming.bss.modules.sfc.entity.Line;
import com.hansming.bss.modules.sfc.entity.LineBranch;
import com.hansming.bss.modules.sfc.entity.LineDatePrice;
import com.hansming.bss.modules.sfc.entity.LineDays;
import com.hansming.bss.modules.sfc.entity.LineDaysDetail;
import com.hansming.bss.modules.sfc.entity.LineExtr;
import com.hansming.bss.modules.sfc.entity.Order;
import com.hansming.bss.modules.sfc.entity.OrderItem;
import com.hansming.bss.modules.sfc.entity.OrderTravellerid;
import com.hansming.bss.modules.sfc.entity.PcUser;
import com.hansming.bss.modules.sfc.entity.Richtext;
import com.hansming.bss.modules.sfc.entity.Traveller;


@Controller
@RequestMapping(value = "/line")
public class LineController extends BaseController{

	@Autowired
	private BannarService bannarservice;
	
	@Autowired
	private CityService cityService;
	
	@Autowired
	private LineService lineService;
	
	@Autowired
	private LineExtrService lineExtrService;
	
	@Autowired
	private LineBranchService lineBranchService;
	
	@Autowired
	private LineDatePriceService lineDatePriceService;
	
	@Autowired
	private LineDaysService lineDaysService;
	
	@Autowired
	private LineDaysDetailService lineDaysDetailService;
	
	@Autowired
	private RichtextService richtextService;
	
	@Autowired
	private ContactsService contactsService;
	
	@Autowired
	private TravellerService travellerService;
	
	@Autowired
	private PcUserService pcUserService;
	
	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private OrderItemService orderItemService;
	
	@Autowired
	private CustomerScoreLogService customerscorelogService;
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private OrderTravelleridService orderTravelleridService;
	
	@Autowired
	private OrderItemService orderitemService;
	
	//线路首页banner接口
	@RequestMapping(value="/bannar")
	public void getbannar(HttpServletRequest request, HttpServletResponse response){
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        try {
			PrintWriter out = response.getWriter();
			JSONObject jsonObject = new JSONObject(); 
	        JSONArray datas = new JSONArray();		//返回到前台的具体的价格日期信息json
			String code = "1";								//返回前台的消息状态
			String msg = "传递成功";
			List<Bannar> bannar = bannarservice.getBannarByType("line");
			for(int i=0;i<bannar.size();i++){
				JSONObject jo = new JSONObject();
				jo.put("picture",Global.getBasePath()+bannar.get(i).getUrl());
//				jo.put("label", bannars.get(i).getLabel());
				datas.put(jo);
			}		
			jsonObject.put("code",code);
			jsonObject.put("msg", msg);
			jsonObject.put("data",datas);
			out.print(jsonObject.toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
	//线路首页线路城市详情信息接口
	@RequestMapping(value="/cityinfo")
	public void cityinfo(HttpServletRequest request, HttpServletResponse response){
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        try {
			PrintWriter out = response.getWriter();
			JSONObject jsonObject = new JSONObject(); 
			JSONObject datas = new JSONObject();		//返回到前台的具体的价格日期信息json
	        JSONArray datashot = new JSONArray();
	        JSONArray datassea = new JSONArray();
	        JSONArray dataswind = new JSONArray();
	        JSONArray dataswater = new JSONArray();
			String code = "1";								//返回前台的消息状态
			String msg = "传递成功";
			List<City> cityhot = cityService.getCitysInlinepage();
			for(int i=0;i<cityhot.size();i++){
				JSONObject jo = new JSONObject();
				jo.put("city", cityhot.get(i).getName());
				datashot.put(jo);
			}
			List<City> citysea = cityService.getthreeCitybyLineType("面朝大海");
			for(int i=0;i<citysea.size();i++){
				JSONObject jo = new JSONObject();
				jo.put("city", citysea.get(i).getName());
				datassea.put(jo);
			}
			List<City> citywind = cityService.getthreeCitybyLineType("民俗风情");
			for(int i=0;i<citywind.size();i++){
				JSONObject jo = new JSONObject();
				jo.put("city", citywind.get(i).getName());
				dataswind.put(jo);
			}
			List<City> citywater = cityService.getthreeCitybyLineType("名山胜水");
			for(int i=0;i<citywater.size();i++){
				JSONObject jo = new JSONObject();
				jo.put("city", citywater.get(i).getName());
				dataswater.put(jo);
			}
					
			datas.put("datashot", datashot);
			datas.put("datassea", datassea);
			datas.put("dataswind", dataswind);
			datas.put("dataswater", dataswater);
			jsonObject.put("code",code);
			jsonObject.put("msg", msg);
			jsonObject.put("data",datas);
			out.print(jsonObject.toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//线路首页线路详情信息接口
		@RequestMapping(value="/indexinfo")
		public void indexinfo(HttpServletRequest request, HttpServletResponse response){
			response.setCharacterEncoding("UTF-8");
			response.setHeader("Access-Control-Allow-Origin", "*");
	        response.setHeader("Cache-Control", "no-cache");
	        try {
				PrintWriter out = response.getWriter();
				JSONObject jsonObject = new JSONObject(); 
				JSONObject datas = new JSONObject();		//返回到前台的具体的价格日期信息json
		        JSONArray datashot = new JSONArray();
		        JSONArray datassea = new JSONArray();
		        JSONArray dataswind = new JSONArray();
		        JSONArray dataswater = new JSONArray();
				String code = "1";								//返回前台的消息状态
				String msg = "传递成功";
				List<Line> linehot = lineService.getindexhotinfo(4);
				for(int i=0;i<linehot.size();i++){
					JSONObject jo = new JSONObject();
					jo.put("city", linehot.get(i).getCity());
					jo.put("title", linehot.get(i).getTitle());
					jo.put("price", linehot.get(i).getPrice());
					jo.put("picture", Global.getBasePath()+linehot.get(i).getShowurl());
					jo.put("lineid", linehot.get(i).getId());
					datashot.put(jo);
				}
				List<Line> linesea = lineService.getindexlineinfo("面朝大海", 8);
				for(int i=0;i<linesea.size();i++){
					JSONObject jo = new JSONObject();
					jo.put("city", linesea.get(i).getCity());
					jo.put("title", linesea.get(i).getTitle());
					jo.put("price", linesea.get(i).getPrice());
					jo.put("picture", Global.getBasePath()+linesea.get(i).getShowurl());
					jo.put("lineid", linesea.get(i).getId());
					datassea.put(jo);
				}
				List<Line> linewind = lineService.getindexlineinfo("民俗风情", 8);
				for(int i=0;i<linewind.size();i++){
					JSONObject jo = new JSONObject();
					jo.put("city", linewind.get(i).getCity());
					jo.put("title", linewind.get(i).getTitle());
					jo.put("price", linewind.get(i).getPrice());
					jo.put("picture", Global.getBasePath()+linewind.get(i).getShowurl());
					jo.put("lineid", linewind.get(i).getId());
					dataswind.put(jo);
				}
				List<Line> linewater = lineService.getindexlineinfo("名山胜水", 8);
				for(int i=0;i<linewater.size();i++){
					JSONObject jo = new JSONObject();
					jo.put("city", linewater.get(i).getCity());
					jo.put("title", linewater.get(i).getTitle());
					jo.put("price", linewater.get(i).getPrice());
					jo.put("picture", Global.getBasePath()+linewater.get(i).getShowurl());
					jo.put("lineid", linewater	.get(i).getId());
					dataswater.put(jo);
				}						
				datas.put("datashot", datashot);
				datas.put("datassea", datassea);
				datas.put("dataswind", dataswind);
				datas.put("dataswater", dataswater);
				jsonObject.put("code",code);
				jsonObject.put("msg", msg);
				jsonObject.put("data",datas);
				out.print(jsonObject.toString());
				out.flush();
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		//线路列表页搜索接口
				@RequestMapping(value="/searchlineinfo")
				public void searchlineinfo(HttpServletRequest request, HttpServletResponse response,String city,Integer days,String starttime,String endtime,Integer startprice,Integer endprice,Integer selecttype,Integer order,Integer page){
					response.setCharacterEncoding("UTF-8");
					response.setHeader("Access-Control-Allow-Origin", "*");
			        response.setHeader("Cache-Control", "no-cache");
			        String startTime = "";
			        String endTime = "";
			        System.out.println("--------------");
			        System.out.println("starttime="+starttime);
			        System.out.println("end="+endtime);
			        if(starttime != null && !starttime.equals("")){
			        	startTime = starttime + "-01";
			        }
			        if(endtime != null && !endtime.equals("")){
//				        处理筛选返程日期，通过计算当月的最大一天来代入返程日期
			        	String[] ey = endtime.split("-");
			        	int endyear = 0;
			        	int endmonth = 0;
			        	try {
			        		endyear = Integer.parseInt(ey[0]);
				        	endmonth = Integer.parseInt(ey[1])-1;	
						} catch (Exception e) {
							// TODO: handle exception
						}
			        			        
				        Calendar cad = Calendar.getInstance();     
				        cad.set(Calendar.YEAR, endyear);     
				        cad.set(Calendar.MONTH, endmonth);     
				        cad.set(Calendar.DAY_OF_MONTH,cad.getActualMaximum(Calendar.DATE));
				        endTime = new   SimpleDateFormat( "yyyy-MM-dd ").format(cad.getTime());
			        }
			        PageBean pagebean = new PageBean();			 
			        pagebean.setPage(page);			        
			        pagebean.setPageSize(10);
			        List<Line> linelist = lineService.getlinelistinfo(city, days, startTime, endTime, startprice, endprice, selecttype, order, pagebean);
			        List<Line> linelistnum = lineService.getlinelistinfoall(city, days, startTime, endTime, startprice, endprice, selecttype, order);
			        int tp = linelistnum.size();
			        pagebean.setTotalCount(tp);
			        try {
						PrintWriter out = response.getWriter();
						JSONObject jsonObject = new JSONObject(); 
						JSONArray datas = new JSONArray();		//返回到前台的具体的价格日期信息json				
						String code = "1";								//返回前台的消息状态
						String msg = "传递成功";
						for(int i=0;i<linelist.size();i++){
							JSONObject jo = new JSONObject();
							jo.put("city", linelist.get(i).getCity());
							jo.put("title", linelist.get(i).getTitle());
							jo.put("picture",Global.getBasePath()+linelist.get(i).getShowurl());
							jo.put("path", linelist.get(i).getPathway());
							jo.put("days", linelist.get(i).getDays());
							jo.put("price", linelist.get(i).getPrice());
							jo.put("lineid", linelist.get(i).getId());
							jo.put("totalpage", pagebean.getTotalPage());
							String idea = linelist.get(i).getIdea();
							String[] ideaa = idea.split("\\|");
							JSONArray jai = new JSONArray();
							for(int a=0;a<ideaa.length;a++){
								JSONObject ji = new JSONObject();
								ji.put("name", ideaa[a]);
								jai.put(ji);
							}
							jo.put("idea", jai);
							String ch = linelist.get(i).getCharacteristic();
							String[] cha = ch.split("\\|");
							JSONArray jac = new JSONArray();
							for(int b=0;b<cha.length;b++){
								JSONObject jc = new JSONObject();
								jc.put("name", cha[b]);
								jac.put(jc);
							}
							jo.put("cha", jac);
							datas.put(jo);
						}
						jsonObject.put("code",code);
						jsonObject.put("msg", msg);
						jsonObject.put("data",datas);
						out.print(jsonObject.toString());
						out.flush();
						out.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
	
	
			//线路详情页线路基本信息接口
			@RequestMapping(value="/lineinfobaseinfo")
			public void lineinfobaseinfo(HttpServletRequest request, HttpServletResponse response,String lineid){
				response.setCharacterEncoding("UTF-8");
				response.setHeader("Access-Control-Allow-Origin", "*");
			    response.setHeader("Cache-Control", "no-cache");
			    try {
					PrintWriter out = response.getWriter();
					JSONObject jsonObject = new JSONObject(); 
					JSONObject datas = new JSONObject();		//返回到前台的具体的价格日期信息json
					JSONArray jch = new JSONArray();
					JSONArray jpa = new JSONArray();
					JSONArray jpi = new JSONArray();
					JSONArray jbr = new JSONArray();
					String code = "1";								//返回前台的消息状态
					String msg = "传递成功";
					Line line = lineService.getlinedetailinfo(lineid);
					LineExtr lineextr = lineExtrService.getLineExtrByLineId(lineid);
					List<LineBranch> lb = lineBranchService.selectByLineid(lineid);
					for(int i = 0;i<lb.size();i++) {
						if(!"".equals(lb.get(i))){
							JSONObject jb = new JSONObject();
							jb.put("car", lb.get(i).getBranchname());
							jb.put("carid", lb.get(i).getId());
							jbr.put(jb);
						}
					}
					
					String pi = lineextr.getPhotos();
					String[] pic = pi.split("\\|");
					for(int i =1;i<pic.length;i++) {
						JSONObject jp = new JSONObject();
						jp.put("name", Global.getBasePath()+pic[i]);
						jpi.put(jp);
					}
					
					String ch = line.getCharacteristic();
					String[] cha = ch.split("\\|");
					for(int i =0;i<cha.length;i++) {
						JSONObject jc = new JSONObject();
						jc.put("name", cha[i]);
						jch.put(jc);
					}
					
					String p = line.getPathway();
					String[] pa = p.split("-");
					for(int i =0;i<pa.length;i++) {
						JSONObject jo = new JSONObject();
						jo.put("name", pa[i]);
						jpa.put(jo);
					}
					datas.put("bran", jbr);
					datas.put("pic", jpi);
					datas.put("cha", jch);
					datas.put("path", jpa);
					datas.put("title", line.getTitle());
					datas.put("city", line.getCity());
					datas.put("days", line.getDays()+"天");
					datas.put("codenum", line.getNum());
					jsonObject.put("code",code);
					jsonObject.put("msg", msg);
					jsonObject.put("data",datas);
					out.print(jsonObject.toString());
					out.flush();
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}	
			//线路详情页价格日历
			@RequestMapping(value="/lineinfodateprice")
			public void lineinfodateprice(HttpServletRequest request, HttpServletResponse response,String time,String carid){
				response.setCharacterEncoding("UTF-8");
				response.setHeader("Access-Control-Allow-Origin", "*");
			    response.setHeader("Cache-Control", "no-cache");
			    try {
					PrintWriter out = response.getWriter();
					JSONObject jsonObject = new JSONObject(); 
					JSONArray datas = new JSONArray();		//返回到前台的具体的价格日期信息json
					Long nowtime = System.currentTimeMillis();
					String[] ta = time.split("-");
					int year = Integer.parseInt(ta[0]);
					int month = Integer.parseInt(ta[1]);
					Calendar nowd = Calendar.getInstance();
					int nowyear = nowd.get(Calendar.YEAR);//当前年
					int nowmonth = nowd.get(Calendar.MONTH)+1;//当前月
					int nowday = nowd.get(Calendar.DATE);//当前日
//					计算当月最大天数
					Calendar ca = Calendar.getInstance();
					ca.clear();
					ca.set(Calendar.YEAR, year);
					ca.set(Calendar.MONTH, month-1);
					int maxday = ca.getActualMaximum(Calendar.DAY_OF_MONTH);
//					计算1号是排在第一行哪个位置
					ca.set(Calendar.DAY_OF_MONTH,1);
					int weekno = ca.get(Calendar.DAY_OF_WEEK);//weekno为月份项数组下标
					if(weekno == 1) {
						weekno = 6;
					}else {
						weekno = weekno-2; 
					}
					LineDatePriceCountHelper cdpch = new LineDatePriceCountHelper();
					if(year == nowyear&&month == nowmonth){//当前年等于传输年及当前月等于传输月时的判断
						for(int i = 0;i<42;i++) {
							JSONObject jo = new JSONObject();
							if(i>=(weekno+nowday)&&i<(weekno+maxday)) {
								String d = "";
								String t = ""+(i-weekno+1);
								if((i-weekno)<9) {
									 d = ""+time+"-0"+(i-weekno+1);
								}else{
									 d = ""+time+"-"+(i-weekno+1);
								}
								LineDatePrice ldp = lineDatePriceService.selectbydateandbranchid(d,carid);
								if(ldp == null){
									jo.put("sybdate", cdpch.CutDay(d));
									jo.put("date", d);
									jo.put("adult", "");
									jo.put("kid", "");
								}else{
									jo.put("date", d);
									jo.put("sybdate", cdpch.CutDay(d));
									jo.put("adult", ldp.getAdultprice());
									jo.put("kid", ldp.getChildprice());
								}						
							}else if(i>=weekno&&i<(weekno+nowday)){
								String d = "";
								String t = ""+(i-weekno+1);
								if((i-weekno)<9) {
									 d = ""+time+"-0"+(i-weekno+1);
								}else{
									 d = ""+time+"-"+(i-weekno+1);
								}
								jo.put("sybdate", cdpch.CutDay(d));
								jo.put("date", d);
								jo.put("adult", "");
								jo.put("kid", "");
							
								}else {
								jo.put("sybdate", " ");
								jo.put("date", "");
								jo.put("adult", "");
								jo.put("kid", "");
							}
							datas.put(jo);
						}
						
					}else if(year<nowyear||(year == nowyear&&month<nowmonth)){//当前年大于传输年时的判断
						for(int i = 0;i<42;i++) {
							JSONObject jo = new JSONObject();
							if(i>=weekno&&i<(weekno+maxday)) {
								String d = "";
								String t = ""+(i-weekno+1);
								if((i-weekno)<9) {
									 d = ""+time+"-0"+(i-weekno+1);
								}else{
									 d = ""+time+"-"+(i-weekno+1);
								}
								LineDatePrice ldp = lineDatePriceService.selectbydateandbranchid(d,carid);
								if(ldp == null){
									jo.put("sybdate", cdpch.CutDay(d));
									jo.put("date", d);
									jo.put("adult", "");
									jo.put("kid", "");
								}else{
									jo.put("sybdate", cdpch.CutDay(d));
									jo.put("date", d);
									jo.put("adult", "");
									jo.put("kid", "");
								}						
							}else {
								jo.put("sybdate", " ");
								jo.put("date", "");
								jo.put("adult", "");
								jo.put("kid", "");
							}
							datas.put(jo);
						}
					}else{//传输的年月比当前年月大
						for(int i = 0;i<42;i++) {
							JSONObject jo = new JSONObject();
							if(i>=weekno&&i<(weekno+maxday)) {
								String d = "";
								String t = ""+(i-weekno+1);
								if((i-weekno)<9) {
									 d = ""+time+"-0"+(i-weekno+1);
								}else{
									 d = ""+time+"-"+(i-weekno+1);
								}
								LineDatePrice ldp = lineDatePriceService.selectbydateandbranchid(d,carid);
								if(ldp == null){
									jo.put("sybdate", cdpch.CutDay(d));
									jo.put("date", d);
									jo.put("adult", "");
									jo.put("kid", "");
								}else{
									jo.put("sybdate", cdpch.CutDay(d));
									jo.put("date", d);
									jo.put("adult", ldp.getAdultprice());
									jo.put("kid", ldp.getChildprice());
								}						
							}else {
								jo.put("sybdate", " ");
								jo.put("date", "");
								jo.put("adult", "");
								jo.put("kid", "");
							}
							datas.put(jo);
						}
					}

					long aftertime = System.currentTimeMillis();
					String usetime = Long.toString(aftertime-nowtime);
					
					String code = "1";								//返回前台的消息状态
					String msg = "传递成功";
					
					jsonObject.put("code",code);
					jsonObject.put("msg", msg);
					jsonObject.put("data",datas);
					jsonObject.put("time", usetime);
					out.print(jsonObject.toString());
					out.flush();
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}	
			//线路详情页线路行程安排接口
			@RequestMapping(value="/lineinfodayarange")
			public void lineinfodayarange(HttpServletRequest request, HttpServletResponse response,String lineid){
				response.setCharacterEncoding("UTF-8");
				response.setHeader("Access-Control-Allow-Origin", "*");
			    response.setHeader("Cache-Control", "no-cache");
			    try {
					PrintWriter out = response.getWriter();
					JSONObject jsonObject = new JSONObject(); 
					JSONArray datas = new JSONArray();		//返回到前台的具体的价格日期信息json
					List<LineDays> linedays = lineDaysService.selectBylineid(lineid);
					for(int i=0;i<linedays.size();i++) {
						JSONArray ja = new JSONArray();
						JSONObject jo = new JSONObject();
						String dayid = linedays.get(i).getId();
						List<LineDaysDetail> linedaysdetail = lineDaysDetailService.selectByDayId(dayid);
						for(int x=0;x<linedaysdetail.size();x++) {
							JSONObject jb = new JSONObject();
							jb.put("time",linedaysdetail.get(x).getTime());
							jb.put("detail",linedaysdetail.get(x).getDetail());
							jb.put("type",linedaysdetail.get(x).getType());
							ja.put(jb);
						}
						jo.put("detail", ja);
						jo.put("day", linedays.get(i).getDay());
						jo.put("dayjourney", linedays.get(i).getDayjourney());
						
						datas.put(jo);
					}
					
					String code = "1";								//返回前台的消息状态
					String msg = "传递成功";
					
					jsonObject.put("code",code);
					jsonObject.put("msg", msg);
					jsonObject.put("data",datas);
					out.print(jsonObject.toString());
					out.flush();
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}	

			//线路详情页线路行程介绍接口
			@RequestMapping(value="/lineinfointro")
			public void lineinfointro(HttpServletRequest request, HttpServletResponse response,String lineid){
				response.setCharacterEncoding("UTF-8");
				response.setHeader("Access-Control-Allow-Origin", "*");
			    response.setHeader("Cache-Control", "no-cache");
			    try {
					PrintWriter out = response.getWriter();
					JSONObject jsonObject = new JSONObject(); 
					JSONArray datas = new JSONArray();		//返回到前台的具体的价格日期信息json
					List<Richtext> rt = richtextService.getRichtextBylineid(lineid);
					LineExtr lx = lineExtrService.getLineExtrByLineId(lineid);
					if(lx != null) {
						
						String richtext = lx.getDetailinformation();
						richtext = richtext.replace("&nbsp;", "　");
				        richtext = richtext.replace("&amp;", "&");
				        richtext = richtext.replace("&#39;", "'");
				        richtext = richtext.replace("&rsquo;", "’");
				        richtext = richtext.replace("&mdash;", "—");
				        richtext = richtext.replace("&ndash;", "–");
				        richtext = richtext.replace("&lt;", "<");
				        richtext = richtext.replace("&gt;", ">");
				        richtext = richtext.replace("&quot;", "\"");
				        richtext = richtext.replace("&ldquo;", "“");
				        richtext = richtext.replace("&rdquo;", "”");
				        richtext = richtext.replace("&hellip;", "...");
				        richtext = richtext.replace("&middot;", "·");
				        richtext = richtext.replace("\"Noto Sans CJK SC\"", "Noto Sans CJK SC");
				        richtext = richtext.replace("\"WenQuanYi Micro Hei\"", "WenQuanYi Micro Hei");
				        richtext = richtext.replace("\"Helvetica Neue\"", "Helvetica Neue");
				        richtext = richtext.replace("</strong >", "</strong>");
				        richtext = richtext.replace("\"Hiragino Sans GB\"", "Hiragino Sans GB");
				        richtext = richtext.replace("\"Microsoft YaHei\"", "Microsoft YaHei");
				        richtext = richtext.replace("src=\"", "src=\""+Global.getBasePath());
				        richtext = richtext.replace("px","rpx");
				        richtext = richtext.replace("\"Lucida Grande\"","Lucida Grande");
				        richtext = richtext.replace("\"Microsoft Yahei\"","Microsoft Yahei");
				        richtext = richtext.replace("\"Microsoft Yahei\"","Microsoft Yahei");
				        richtext = richtext.replace("\"Source Han Sans SC\"","Source Han Sans SC");
				        richtext = richtext.replace("\"Hiragino Sans GB W3\"","Hiragino Sans GB W3");
				        richtext = richtext.replace("\"PingFang SC\"","PingFang SC");
				        richtext = richtext.replace("&nbsp;", "　");
				        richtext = richtext.replace("<img", "<img class='img-responsive center-block'");
				        richtext = richtext.replace("<section>", "<div>");
				        richtext = richtext.replace("</section>", "</div>");
				        richtext = richtext.replace("width","height");
	
						JSONObject jo = new JSONObject();						
						jo.put("infodetail", richtext);
						jo.put("sign", "0");
						datas.put(jo);
					}else {
					for(int i=0;i<rt.size();i++) {
						JSONObject jo = new JSONObject();
						if(rt.get(i).getType().equals("图片")){
							jo.put("type", rt.get(i).getType());
							jo.put("detail", Global.getrichtextpicture()+rt.get(i).getDetail());
							jo.put("sign", "1");
						}else{
							jo.put("type", rt.get(i).getType());
							jo.put("detail", rt.get(i).getDetail());
							jo.put("sign", "1");
						}
						datas.put(jo);
						}
					}
//					for(int i=0;i<rt.size();i++) {
//						JSONObject jo = new JSONObject();
//						if(rt.get(i).getType().equals("图片")){
//							jo.put("type", rt.get(i).getType());
//							jo.put("detail", Global.getrichtextpicture()+rt.get(i).getDetail());
//						}else{
//							jo.put("type", rt.get(i).getType());
//							jo.put("detail", rt.get(i).getDetail());
//						}
//						datas.put(jo);
//					}
					String code = "1";								//返回前台的消息状态
					String msg = "传递成功";				
					jsonObject.put("code",code);
					jsonObject.put("msg", msg);
					jsonObject.put("data",datas);
					out.print(jsonObject.toString());
					out.flush();
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			
			//线路详情页线路预定须知接口
			@RequestMapping(value="/lineinforule")
			public void lineinforule(HttpServletRequest request, HttpServletResponse response,String lineid){
				response.setCharacterEncoding("UTF-8");
				response.setHeader("Access-Control-Allow-Origin", "*");
			    response.setHeader("Cache-Control", "no-cache");
			    try {
					PrintWriter out = response.getWriter();
					JSONObject jsonObject = new JSONObject(); 
					JSONArray datas = new JSONArray();		//返回到前台的具体的价格日期信息json
					List<Richtext> rt = richtextService.getRichtextpreinfoBylineid(lineid);
					for(int i=0;i<rt.size();i++) {
						JSONObject jo = new JSONObject();
						jo.put("type", rt.get(i).getType());
						jo.put("detail", rt.get(i).getDetail());
						datas.put(jo);
					}
					String code = "1";								//返回前台的消息状态
					String msg = "传递成功";
					
					jsonObject.put("code",code);
					jsonObject.put("msg", msg);
					jsonObject.put("data",datas);
					out.print(jsonObject.toString());
					out.flush();
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			//线路填写订单页联系人信息接口
			@RequestMapping(value="/lineordercontact")
			public void lineordercontact(HttpServletRequest request, HttpServletResponse response,String token){
				response.setCharacterEncoding("UTF-8");
				response.setHeader("Access-Control-Allow-Origin", "*");
			    response.setHeader("Cache-Control", "no-cache");
			    try {
					PrintWriter out = response.getWriter();
					JSONObject jsonObject = new JSONObject(); 
					JSONArray datas = new JSONArray();		//返回到前台的具体的价格日期信息json
					List<Contacts> ct = contactsService.getContactsByToken(token);
					for(int i = 0;i<ct.size();i++){
						JSONObject jo = new JSONObject();
						jo.put("name", ct.get(i).getCname());
						jo.put("identtype", ct.get(i).getIdenttype());
						jo.put("identnum", ct.get(i).getIdentnum());
						jo.put("email", ct.get(i).getEmail());
						jo.put("phone", ct.get(i).getPhone());
						jo.put("contactid", ct.get(i).getId());
						datas.put(jo);
					}
					String code = "1";								//返回前台的消息状态
					String msg = "传递成功";
					
					jsonObject.put("code",code);
					jsonObject.put("msg", msg);
					jsonObject.put("data",datas);
					out.print(jsonObject.toString());
					out.flush();
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			//线路填写订单页联系人信息接口
			@RequestMapping(value="/lineordercontactinfo")
			public void lineordercontactinfo(HttpServletRequest request, HttpServletResponse response,String id){
				response.setCharacterEncoding("UTF-8");
				response.setHeader("Access-Control-Allow-Origin", "*");
			    response.setHeader("Cache-Control", "no-cache");
			    try {
					PrintWriter out = response.getWriter();
					JSONObject jsonObject = new JSONObject(); 
					JSONArray datas = new JSONArray();		//返回到前台的具体的价格日期信息json
					Contacts ct = contactsService.get(id);
					JSONObject jo = new JSONObject();
					jo.put("name",ct.getCname());
					jo.put("phone",ct.getPhone());
					jo.put("contactid", ct.getId());
					if(ct.getEmail() != null && !ct.getEmail().equals("")){
						jo.put("email", ct.getEmail());
					}else{
						jo.put("email", "");
					}
					datas.put(jo);
					String code = "1";								//返回前台的消息状态
					String msg = "传递成功";
					
					jsonObject.put("code",code);
					jsonObject.put("msg", msg);
					jsonObject.put("data",datas);
					out.print(jsonObject.toString());
					out.flush();
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			//线路填写订单页查找出游人信息接口
			@RequestMapping(value="/lineordertravellist")
			public void lineordertravellist(HttpServletRequest request, HttpServletResponse response,String token){
				response.setCharacterEncoding("UTF-8");
				response.setHeader("Access-Control-Allow-Origin", "*");
			    response.setHeader("Cache-Control", "no-cache");
			    try {
					PrintWriter out = response.getWriter();
					JSONObject jsonObject = new JSONObject(); 
					JSONArray datas = new JSONArray();		//返回到前台的具体的价格日期信息json
					List<Traveller> tr = travellerService.selectTravellerByToken(token);
					if(tr != null && !tr.isEmpty()){
						for(int i=0;i<tr.size();i++) {
							JSONObject jo = new JSONObject();
							jo.put("name", tr.get(i).getName());
							jo.put("identtype", tr.get(i).getIdenttype());
							jo.put("identnum", tr.get(i).getIdentnum());
							jo.put("type", tr.get(i).getType());
							jo.put("gender", tr.get(i).getGender());
							jo.put("phone", tr.get(i).getPhone());
							jo.put("birthday", tr.get(i).getBirthday());
							jo.put("limiti", tr.get(i).getLimitidentdate());
							jo.put("identcity", tr.get(i).getIdentcity());
							jo.put("surname", tr.get(i).getSurname());
							jo.put("ename", tr.get(i).getEname());
							jo.put("nationality", tr.get(i).getNationality());
							jo.put("id", tr.get(i).getId());
							datas.put(jo);
						}
					}else{
						
					}					
					String code = "1";								//返回前台的消息状态
					String msg = "传递成功";
					
					jsonObject.put("code",code);
					jsonObject.put("msg", msg);
					jsonObject.put("data",datas);
					out.print(jsonObject.toString());
					out.flush();
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			//线路填写订单页查找出游人信息接口
			@RequestMapping(value="/lineordertravellistinfo")
			public void lineordertravellistinfo(HttpServletRequest request, HttpServletResponse response,String id){
				response.setCharacterEncoding("UTF-8");
				response.setHeader("Access-Control-Allow-Origin", "*");
			    response.setHeader("Cache-Control", "no-cache");
			    try {
					PrintWriter out = response.getWriter();
					JSONObject jsonObject = new JSONObject(); 
					JSONArray datas = new JSONArray();		//返回到前台的具体的价格日期信息json
					Traveller tr = travellerService.get(id);
					JSONObject jo = new JSONObject();
					jo.put("name", tr.getName());
					jo.put("identtype", tr.getIdenttype());
					jo.put("identnum", tr.getIdentnum());
					jo.put("type", tr.getType());
					jo.put("gender", tr.getGender());
					jo.put("phone", tr.getPhone());
					jo.put("birthday", tr.getBirthday());
					jo.put("limiti", tr.getLimitidentdate());
					jo.put("identcity", tr.getIdentcity());
					jo.put("surname", tr.getSurname());
					jo.put("ename", tr.getEname());
					jo.put("nationality", tr.getNationality());
					jo.put("id", tr.getId());
					datas.put(jo);
					String code = "1";								//返回前台的消息状态
					String msg = "传递成功";
					
					jsonObject.put("code",code);
					jsonObject.put("msg", msg);
					jsonObject.put("data",datas);
					out.print(jsonObject.toString());
					out.flush();
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			
			//线路填写订单页增加或者更新出游人接口,返回contactid,既sfc_traveller中id
			@RequestMapping(value="/lineordertravelinsert")
			public void lineordertravelinsert(HttpServletRequest request, HttpServletResponse response,String token,String name,String identtype,String identnum,String phone,String contactid){
				response.setCharacterEncoding("UTF-8");
				response.setHeader("Access-Control-Allow-Origin", "*");
			    response.setHeader("Cache-Control", "no-cache");
			    try {
					PrintWriter out = response.getWriter();
					JSONObject jsonObject = new JSONObject(); 
					JSONObject datas = new JSONObject();		//返回到前台的具体的价格日期信息json
					//讲在pcuser表中根据token查找到的userid放入Customer类中，将Customer放入Traveller中，使之取得userid
					HttpSession session = request.getSession();
					String token1 = (String)session.getAttribute("token");
//					PcUser pc = pcUserService.selectAllByToken(token1);
//					String userid = pc.getUserid();
//					Customer ct = new Customer();
//					ct.setId(userid);
					Traveller tr = new Traveller();
//					tr.setCustomer(ct);
					
					tr.setName(name);
					tr.setIdenttype(identtype);
					tr.setIdentnum(identnum);
					tr.setPhone(phone);
					if(contactid != null && !contactid.equals("")) {
						tr.setId(contactid);
						travellerService.pcupdatetraveller(tr);
						datas.put("contactid", contactid);
					}else {
						String id = IdGen.uuid();
						tr.setId(id);
						travellerService.insertTravellerPcNew(tr);
						datas.put("contactid", id);
					}
					
					String code = "1";								//返回前台的消息状态
					String msg = "传递成功";					
					jsonObject.put("code",code);
					jsonObject.put("msg", msg);
					jsonObject.put("data",datas);
					out.print(jsonObject.toString());
					out.flush();
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			//线路填写订单页面不计算积分时儿童和成人价格接口
			@RequestMapping(value="/lineorderpriceall")
			public void lineorderpriceall(HttpServletRequest request, HttpServletResponse response,String linecarid,String linetime){
				response.setCharacterEncoding("UTF-8");
				response.setHeader("Access-Control-Allow-Origin", "*");
			    response.setHeader("Cache-Control", "no-cache");
			    try {
					PrintWriter out = response.getWriter();
					JSONObject jsonObject = new JSONObject(); 
					JSONObject datas = new JSONObject();		//返回到前台的具体的价格日期信息json
					LineDatePrice ldp = lineDatePriceService.selectBybridandtime(linecarid, linetime);
					datas.put("adultprice", ldp.getAdultprice());
					datas.put("kidspirce", ldp.getChildprice());
					String code = "1";								//返回前台的消息状态
					String msg = "传递成功";					
					jsonObject.put("code",code);
					jsonObject.put("msg", msg);
					jsonObject.put("data",datas);
					out.print(jsonObject.toString());
					out.flush();
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			//线路订单支付页面全部内容接口
			@RequestMapping(value="/linepayalldetailinfo")
			public void linepayalldetailinfo(HttpServletRequest request, HttpServletResponse response,String orderid){
				response.setCharacterEncoding("UTF-8");
				response.setHeader("Access-Control-Allow-Origin", "*");
			    response.setHeader("Cache-Control", "no-cache");
			    try {
					PrintWriter out = response.getWriter();
					JSONObject jsonObject = new JSONObject(); 
					JSONObject datas = new JSONObject();		//返回到前台的具体的价格日期信息json
					JSONArray jadult = new JSONArray();
					JSONArray jkid = new JSONArray();
					Order order = new Order();
					order.setId(orderid);
					Order od = orderService.selectOrderByid(order);
					Line li = od.getLine();
					String lineid = li.getId();
					Line line = lineService.get(lineid);
					List<OrderTravellerid> otadult = orderTravelleridService.getadultTravelleridByOrderid(orderid);
					List<OrderTravellerid> otkid = orderTravelleridService.getkidTravelleridByOrderid(orderid);
					for(int i =0;i<otadult.size();i++){
						String travellerid  = otadult.get(i).getTravellerid();
						Traveller tr = travellerService.get(travellerid);
						JSONObject jo = new JSONObject();
						jo.put("name", tr.getName());
						jo.put("identnum", tr.getIdentnum());
						if(tr.getPhone() != null && !tr.getPhone().equals("")){
							jo.put("phone", tr.getPhone());
						}else{
							jo.put("phone", "未提供");
						};
						jadult.put(jo);
					}
					for(int i =0;i<otkid.size();i++){
						String travellerid  = otkid.get(i).getTravellerid();
						Traveller tr = travellerService.get(travellerid);
						JSONObject jo = new JSONObject();
						jo.put("name", tr.getName());
						jo.put("identnum", tr.getIdentnum());
						jkid.put(jo);
					}
					int adult = od.getAdult();
					int kids = od.getChild();
					String adultnum = adult + "成人";
					String kidsnum = kids + "儿童";
			
					
					
					
					
					datas.put("title", od.getProductname());
					datas.put("ordernum", od.getNum());
					datas.put("car", od.getBranchname());
					datas.put("city", line.getCity());
					datas.put("begindate", od.getBeginDate());
					datas.put("adultnum", adultnum);
					datas.put("kidsnum", kidsnum);
					datas.put("cname", od.getCname());
					datas.put("phone", od.getPhone());
					datas.put("email", od.getEmail());
					datas.put("adult", jadult);
					datas.put("kids", jkid);
					datas.put("picture", Global.getBasePath()+line.getShowurl());
					datas.put("price", od.getPrice());
					
					String code = "1";								//返回前台的消息状态
					String msg = "传递成功";					
					jsonObject.put("code",code);
					jsonObject.put("msg", msg);
					jsonObject.put("data",datas);
					out.print(jsonObject.toString());
					out.flush();
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			//线路填写订单页创建订单接口
			@RequestMapping(value="/lineordercreateorder")
			public void lineordercreateorder(HttpServletRequest request, HttpServletResponse response,String token,String name,String phone,String email,String message,String cid,String travellerid,Integer adultnum,Integer kidsnum,String lineid,String linetime,String linecarid,Integer score) throws Exception{
				response.setCharacterEncoding("UTF-8");
				response.setHeader("Access-Control-Allow-Origin", "*");
			    response.setHeader("Cache-Control", "no-cache");
			    try {
					PrintWriter out = response.getWriter();
					JSONObject jsonObject = new JSONObject(); 
					JSONObject datas = new JSONObject();		//返回到前台的具体的价格日期信息json
					String code = "1";								//返回前台的消息状态
					String msg = "传递成功";					
					
					Line line = lineService.get(lineid);
					//计算订单终止时间
					Date now = new Date();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			      	Calendar todatecal = Calendar.getInstance();
			      	todatecal.setTime(sdf.parse(linetime));
			      	todatecal.add(Calendar.DAY_OF_MONTH,line.getDays());
			      	Date enddate = todatecal.getTime();
			      	String endd = sdf.format(enddate);
			      	//根据token得到用户
			      	HttpSession session = request.getSession();
			      	String token1 = (String) session.getAttribute("token");
			      	PcUser pcu = pcUserService.selectAllByToken(token1);
			      	String uid = pcu.getUserid();
			      	Customer user = customerService.get(uid);
			      	//写入订单数据
			      	Order order = new Order();
			      	order.setOrderNum();
			      	order.setId(IdGen.uuid());
			      	order.setState("1");
			      	order.setType("2");
			      	order.setLine(line);
			      	order.setAdult(adultnum);
			      	order.setChild(kidsnum);
			      	order.setBeginDate(linetime);
			      	order.setEndDate(endd);
					LineBranch linebranch = lineBranchService.get(linecarid);
					order.setBranchname(linebranch.getBranchname());
					order.setProductname(line.getTitle());
					//订单联系人信息设置
					order.setCname(name);
					order.setPhone(phone);
					order.setEmail(email);
					order.setCreateDate(now);
					//订单留言
					order.setCusmsg(message);
					order.setCustomer(user);
					//强制更新或者储存联系人信息
					Contacts contacts = new Contacts();
					if(cid != null && !cid.equals("")){
						contacts.setId(cid);
					};
					contacts.setCustomer(user);
					contacts.setCname(name);
					contacts.setPhone(phone);
					contacts.setEmail(email);
					contactsService.save(contacts);
					//orderitem订单记录明细
					String priceinfo="";
					BigDecimal price = new BigDecimal("0.00");
					LineDatePrice linedateprice = lineDatePriceService.selectbydateandbranchid(linetime, linecarid);
					BigDecimal adultprice = linedateprice.getAdultprice().multiply(new BigDecimal(adultnum));
					if(adultnum != 0){
						OrderItem orderitemadult = new OrderItem();
						orderitemadult.setItemname("成人"+adultnum+"人");
						orderitemadult.setItemprice(adultprice);
						orderitemadult.setOrder(order);
						orderItemService.save(orderitemadult);
						priceinfo += "成人"+adultnum+"人"+adultprice+"元";
					}
					BigDecimal kidsprice = linedateprice.getChildprice().multiply(new BigDecimal(kidsnum));
					if(kidsnum != 0){
						OrderItem orderitemkids = new OrderItem();
						orderitemkids.setItemname("儿童"+kidsnum+"人");
						orderitemkids.setItemprice(kidsprice);
						orderitemkids.setOrder(order);
						orderItemService.save(orderitemkids);
						priceinfo += "儿童"+kidsnum+"人"+kidsprice+"元";
					}
					price = adultprice.add(kidsprice);
					//将travellerid orderid及信息填写进sfc_order_traveller表中
					String[] tids = travellerid.split("\\|");
					for(int i=0;i<tids.length;i++){
						if(i<=adultnum){
							if(!"".equals(tids[i])){
								travellerService.saveTraveInOrder(tids[i], "成人", order.getId());
							}
						}else{
							if(!"".equals(tids[i])){
							    travellerService.saveTraveInOrder(tids[i], "儿童", order.getId());
							}
						}
					}
					//计算订单积分
					if(score <= user.getScore() && score > 0){
						if(score<100000){
							if(score/100<price.intValue() ){//使用积分抵扣的钱小于总价
								BigDecimal bscore = new BigDecimal(score);					
								BigDecimal dscore =bscore.divide(new BigDecimal(100),2,RoundingMode.HALF_UP) ;
								price = price.subtract(dscore);
								//添加服务项
								OrderItem orderitem5 = new OrderItem();
								orderitem5.setItemname("积分抵扣");
								orderitem5.setItemprice(dscore);
								orderitem5.setOrder(order);
								orderItemService.save(orderitem5);
								//增加积分使用日志
								CustomerScoreLog csl = new CustomerScoreLog();					
								csl.setDatetime(sdf.format(now));
								csl.setScore(score);
								csl.setDetail("订购"+line.getTitle());
								csl.setType("2");
								csl.setCustomer(user);
								customerscorelogService.save(csl);
								//改变积分
								user.setScore(user.getScore()-score);
								customerService.updatescore(user);
								priceinfo += "积分抵扣"+dscore+"元  ";
							}else{//使用积分抵扣的钱大于总价
								int costscore = price.multiply(new BigDecimal("100.00")).intValue();
								BigDecimal money = new BigDecimal("1000.00");
								price =  price.subtract(money);						
								OrderItem orderitem5 = new OrderItem();
								orderitem5.setItemname("积分抵扣");
								orderitem5.setItemprice(price);
								orderitem5.setOrder(order);
								orderItemService.save(orderitem5);
								
								CustomerScoreLog csl = new CustomerScoreLog();
								csl.setDatetime(sdf.format(now));
								csl.setScore(100000);
								csl.setDetail("订购"+line.getTitle());
								csl.setType("2");
								csl.setCustomer(user);
								customerscorelogService.save(csl);
								user.setScore(user.getScore()-costscore);
								customerService.updatescore(user);
								priceinfo += "积分抵扣"+money+"元  ";
							}
						}else{
							if(1000 < price.intValue()){
								BigDecimal dscore = new BigDecimal("1000");
								price = price.subtract(dscore);
								OrderItem orderitem5 = new OrderItem();
								orderitem5.setItemname("积分抵扣");
								orderitem5.setItemprice(dscore);
								orderitem5.setOrder(order);
								orderItemService.save(orderitem5);
								CustomerScoreLog csl = new CustomerScoreLog();					
								csl.setDatetime(sdf.format(now));
								csl.setScore(100000);
								csl.setDetail("订购"+line.getTitle());
								csl.setType("2");
								csl.setCustomer(user);
								customerscorelogService.save(csl);
								user.setScore(user.getScore()-100000);
								customerService.updatescore(user);
								priceinfo += "积分抵扣"+dscore+"元  ";
							}else{
								int costscore = price.multiply(new BigDecimal("100.00")).intValue();
													
								OrderItem orderitem5 = new OrderItem();
								orderitem5.setItemname("积分抵扣");
								orderitem5.setItemprice(price);
								orderitem5.setOrder(order);
								orderItemService.save(orderitem5);
								
								
								
								CustomerScoreLog csl = new CustomerScoreLog();
								csl.setDatetime(sdf.format(now));
								csl.setScore(costscore);
								csl.setDetail("订购"+line.getTitle());
								csl.setType("2");
								csl.setCustomer(user);
								customerscorelogService.save(csl);
								user.setScore(user.getScore()-costscore);
								customerService.updatescore(user);
								priceinfo += "积分抵扣"+price+"元 ";
								price =  price.subtract(price);	
							}
							
						}
					}
					if(price.compareTo(new BigDecimal("0.00"))<=0){
						price = new BigDecimal("0.00");
						PhoneMsgUtil.sendSingleSms(order.getPhone(), order.getNum());
						code = "2";
						order.setState("2");
					}
					order.setPrice(price);
			      	orderService.savelineorder(order);
			      	PropertiesLoader config = new PropertiesLoader("config.properties");
					String yunyingemail = config.getProperty("yunyingemail");
			      	try {
			      		SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd 	HH:mm:ss");
						String emailtile = "小程序顾客创建旅游线路待支付订单";
						String content = "<p>时间："+dft.format(now)+"</p>"+								
													"<p>订单号:"+order.getNum()+"</p>"+
													"<p>填写的联系人名称:"+name+"</p>"+
													"<p>填写的联系人手机:"+phone+"</p>"+
													"<p>填写的联系人邮箱:"+email+"</p>"+
													"<p>线路名称:"+line.getTitle()+" "+line.getCity()+"  "+linebranch.getBranchname()+"</p>"+
													"<p>发团时间:"+linetime+"</p>"+
													"<p>订单详情;</p>"+
													"<p>"+priceinfo+"</p>"+
													"<p>总价为:"+price+"</p>"+
													"<p>用户留言:"+message+"</p>";
													
						SendMailUtil.Sendmail(yunyingemail,emailtile , content);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						System.out.println("发送邮件失败");
					}
			      	datas.put("orderid", order.getId());
			      	session.setAttribute("orderid", order.getId());
					jsonObject.put("code",code);
					jsonObject.put("msg", msg);
					jsonObject.put("data",datas);
					out.print(jsonObject.toString());
					out.flush();
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			
			@RequestMapping(value="/lineorderinfo")
			public void getCarOrderInfo(HttpServletResponse response,String orderid) throws IOException, JSONException, ParseException{
				response.setCharacterEncoding("UTF-8");
		        response.setHeader("Content-type", "text/html;charset=UTF-8");
		        PrintWriter out = response.getWriter();
			        JSONObject jsonObject = new JSONObject();
			    JSONObject datas = new  JSONObject();
			        String code = "1";								//返回前台的消息状态
			    String msg = "传输成功";								//返回前台的消息
				   Order order =orderService.get(orderid);
				   if(order != null)
			        {
					   if(order.getType().equals("2")){
						   Line line = lineService.get(order.getLine().getId());
						   datas.put("state", order.getState());
							datas.put("num",order.getNum() );		
							datas.put("adult", order.getAdult());
							datas.put("child", order.getChild());
							datas.put("car", order.getBranchname());
							datas.put("cusmsg", order.getCusmsg());
							//联系人信息
							datas.put("name",order.getCname() );
							datas.put("email", order.getEmail());
							datas.put("phone",order.getPhone());
							
							datas.put("begindate",order.getBeginDate());
							datas.put("beginday",TimeUtil.getweekday(order.getBeginDate()));
							datas.put("enddate",order.getEndDate());
							datas.put("endday",TimeUtil.getweekday(order.getEndDate()));
							datas.put("begintime", CarTimeHelp.changetotime(order.getBegintime()));
							datas.put("endtime", CarTimeHelp.changetotime(order.getEndtime()));
							datas.put("cusmsg", order.getCusmsg());
							//线路详情
							datas.put("title", line.getTitle());
							datas.put("city",line.getCity());
							datas.put("imgurl",Global.getBasePath()+line.getShowurl());
							//游客信息
							List<Traveller> travellers = travellerService.findTravellersByOrderId(orderid);
							JSONArray traers = new JSONArray();
							int i =1;
							for(Traveller t:travellers){
								if(t != null){
									JSONObject j = new JSONObject();
									j.put("name", t.getName());
									j.put("type",t.getType());
									j.put("num", t.getIdentnum());
									j.put("id", i);
									i++;
									j.put("phone", t.getPhone());
									traers.put(j);
								}
							}
							
							//订单详情
							List<OrderItem> lo = orderitemService.getOrderItemsByOrderId(orderid);
							JSONArray ja = new JSONArray();
							for(OrderItem oi:lo){
								if(!"积分抵扣".equals(oi.getItemname())){
									JSONObject jsono = new JSONObject();
									jsono.put("name", oi.getItemname());
									jsono.put("price", oi.getItemprice());
									ja.put(jsono);
								}else{
									datas.put("scorecost", oi.getItemprice());
								}
							}
							
							datas.put("travellers", traers);
							datas.put("orderdetail",ja);
							datas.put("price", order.getPrice());
					   }else{
						   code="2";
			    			msg = "订单获取类别错误";
					   }
			        }else{
			        	code ="2";
			        	msg = "订单获取失败请联系客服人员！";
			        }
				   	jsonObject.put("code", code);
					jsonObject.put("msg", msg);
					jsonObject.put("data", datas);
					out.print(jsonObject.toString());
					out.flush();
					out.close();
			}
}
